package com.ruoyi.web.controller.cs;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.constant.Constants;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.Response;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.core.domain.model.LoginUser;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.ServletUtils;
import com.ruoyi.cs.service.IHCPayService;
import com.ruoyi.framework.web.service.TokenService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.math.BigDecimal;
import java.util.Map;

@RestController
@RequestMapping("/cs/hcpay")
@Api(tags = {"汇潮支付 接口文档"})
public class HCpayController extends BaseController {
    private static final Logger log = LoggerFactory.getLogger(HCpayController.class);

    @Autowired
    private TokenService tokenService;

    @Autowired
    private IHCPayService ihcPayService;


    @ApiOperation("汇潮支付PC端")
    @RequestMapping(name = "汇潮支付PC端", value = "hcpayPayment", method = RequestMethod.GET)
    @Log(title = "汇潮支付PC端", businessType = BusinessType.INSERT)
    public Response<Map> hcpayPayment(
            @ApiParam(name = "czMoney", value = "充值金币", required = true)
            @RequestParam(name = "czMoney", required = true) BigDecimal czMoney,
            @ApiParam(name = "czEquipment", value = "充值设备 0 PC 1 H5", required = true)
            @RequestParam(name = "czEquipment", required = true) String czEquipment) throws Exception {
        LoginUser loginUser = tokenService.getLoginUserWeb(ServletUtils.getRequest());
        SysUser user = loginUser.getUser();
        String userId = user.getLoginUserId();
        try {
            return ihcPayService.recharge(userId, czMoney, czEquipment);
        } catch (Exception e) {
            log.error("汇潮支付PC端充值接口异常:{},参数:{}", e.getMessage());
            return Response.fail("系统繁忙，请稍候再试!");
        }
    }

    @ApiOperation("汇潮支付异步调用")
    @RequestMapping(name = "汇潮支付异步调用", value = "hcPayNotify", method = RequestMethod.POST)
    @Log(title = "web汇潮支付异步调用", businessType = BusinessType.INSERT)
    public String hcPayNotify(HttpServletRequest request) {
        return ihcPayService.hcPayNotify(request);
    }

    @ApiOperation("汇潮支付扫码跳转")
    @RequestMapping(name = "汇潮支付扫码跳转", value = "paySM", method = RequestMethod.GET)
    @Log(title = "汇潮支付扫码跳转", businessType = BusinessType.INSERT)
    public ModelAndView paySM(String uuid, HttpServletResponse response) {
        String url = ihcPayService.paySM(uuid);
        logger.info("汇潮支付扫码参数:{},完整地址:{}", uuid, url);
        try {
            ModelAndView mav = new ModelAndView("redirect:" + "https://paygw.yemadai.com/scan/merchant/dopay");
            mav.addObject("requestDomain", url);
            return mav;
        } catch (Exception e) {
            logger.error("汇潮支付扫码异常:{},错误信息:{}", uuid, e.getMessage());
            return null;
        }
    }

}
